package com.example.housemanage

import android.view.Menu
import android.view.MenuItem
import android.widget.PopupMenu
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.housemanage.databinding.ActivityHouseManageBinding
import com.example.housemanage.entity.HouseManageEntityItem
import com.example.lib_base.mvvm.BaseActivity
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/housemanage/HouseManageActivity")
class HouseManageActivity : BaseActivity<ActivityHouseManageBinding, HouseManageViewModel>() {
    // 楼栋分组数据结构
    data class BuildingGroup(
        val buildingName: String,
        val area: Int,
        val rooms: List<HouseManageEntityItem>,
        var expanded: Boolean = true // 默认展开
    )

    private lateinit var buildingAdapter: BuildingAdapter
    
    /**
     * 删除单个房间
     * @param room 要删除的房间
     */
    fun deleteRoom(room: HouseManageEntityItem) {
        val content = "${room.id}室\n企业: ${room.enterpriseName}\n面积: ${room.area}m²"
        android.app.AlertDialog.Builder(this)
            .setTitle("确认删除房间")
            .setMessage(content)
            .setNegativeButton("取消") { _, _ -> vm.getProperties() } // 刷新数据
            .setPositiveButton("确定") { _, _ ->
                vm.deleteProperty(room.id)
            }
            .show()
    }

override fun initView() {
    binding.rvBuilding.layoutManager = LinearLayoutManager(this)
    // 添加右上角添加按钮
    binding.toolbar.inflateMenu(R.menu.menu_house_manage)
    binding.toolbar.setOnMenuItemClickListener { item ->
        if (item.itemId == R.id.action_add) {
            // 弹出菜单
            val popup = PopupMenu(this, binding.toolbar)
            popup.menu.add("添加房源信息").setOnMenuItemClickListener {
                ARouter.getInstance().build("/housemanage/add/AddHouseActivity").navigation()
                true
            }
            popup.show()
            true
        } else {
            false
        }
    }
    binding.toolbar.setOnClickListener {
        finish()
    }

}


    override fun initData() {
        vm.propertiesResult.observe(this) { list ->
            // 按 regionName 分组为楼栋
            val groupMap = list.groupBy { it.regionName }
            val buildingList = groupMap.map { (region, rooms) ->
                BuildingGroup(
                    buildingName = region,
                    area = rooms.sumOf { it.area }, // 可自定义面积逻辑
                    rooms = rooms
                )
            }.toMutableList()
            buildingAdapter = BuildingAdapter(buildingList, this)
            binding.rvBuilding.adapter = buildingAdapter
        }
        vm.getProperties()
        // 监听删除结果
        vm.deletePropertyResult.observe(this) { success ->
            if (success) {
                ToastUtils.showLong("删除成功")
                vm.getProperties()
            } else {
                ToastUtils.showLong("删除失败，请重试")
            }
        }
    }

    override fun onResume() {
        super.onResume()
        vm.getProperties()
    }
    //防止内存泄漏
    override fun onDestroy() {
        super.onDestroy()
        vm.deletePropertyResult.removeObservers(this)
        vm.propertiesResult.removeObservers(this)
        vm.addPropertyResult.removeObservers(this)
        vm.updatePropertyResult.removeObservers(this)

    }
}